package zasm.zasm.util;

import java.util.Iterator;

public class SingletonIterable<E> implements Iterable<E> {

	private final E	singleton;

	public SingletonIterable(final E singleton) {
		this.singleton = singleton;
	}

	@Override
	public final Iterator<E> iterator() {
		return new SingletonIterator<>(singleton);
	}

	@Override
	public final int hashCode() {
		return singleton == null ? 0 : singleton.hashCode();
	}

	@Override
	public final boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof SingletonIterable)) {
			return false;
		}
		SingletonIterable<?> other = (SingletonIterable<?>) obj;
		if (singleton == null) {
			if (other.singleton != null) {
				return false;
			}
		} else if (!singleton.equals(other.singleton)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "[" + singleton + "]";
	}

}
